<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>mouse move drag</title>
  <style>
    .mouse-move {
      width: 100px;
      height: 100px;
      background: #ff6666;
      position: absolute;
    }
  </style>
  <script type="text/javascript">
    window.onload = function () {
      var odiv = document.querySelector('.mouse-move')

      odiv.onmousedown = function (ev) {
        var events = ev || event
        var disX = events.clientX - odiv.offsetLeft
        var disY = events.clientY - odiv.offsetTop
        var l = 0;
        var t = 0;

        document.onmousemove = function (ev) {
          var events = ev || event
          l = events.clientX - disX <= 0 ? 0 : events.clientX - disX
          t = events.clientY - disY <= 0 ? 0 : events.clientY - disY

          if (l >= document.documentElement.clientWidth - odiv.offsetWidth) {
            l = document.documentElement.clientWidth - odiv.offsetWidth
          }

          if (t >= document.documentElement.clientHeight - odiv.offsetHeight) {
            t = document.documentElement.clientHeight - odiv.clientHeight
          }
          odiv.style.left = l + 'px'
          odiv.style.top = t + 'px'
        }

        document.onmouseup = function () {
          document.onmousemove = null
          document.onmouseup = null
        }
        return false
      }
    }
  </script>
</head>
<body>
  <div class="mouse-move"></div>
</body>
</html>